package com.niiwoo.civet.mobile.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.mobile.vo.request.EncryptionToDecryptRequestVO;
import com.niiwoo.civet.mobile.vo.request.SignContractRequestVO;
import com.niiwoo.civet.mobile.vo.request.PrizeCompensateRequestVO;
import com.niiwoo.civet.mobile.vo.response.EncryptionToDecryptResponseVO;
import com.niiwoo.civet.mobile.vo.response.PlatformStatisticsResponseVo;
import com.niiwoo.civet.mobile.vo.response.platform.ProjectIntroduceVO;
import com.niiwoo.civet.trade.dto.response.ProjectIntroduceDTO;
import com.niiwoo.civet.trade.dto.response.statisticsinfo.StatisticsInfoResponseDTO;
import com.niiwoo.civet.trade.service.configure.TradeConfigDubboService;
import com.niiwoo.civet.trade.service.lend.ProjectLendDubboService;
import com.niiwoo.civet.trade.service.statisticsinfo.StatisticsInfoDubboService;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import com.niiwoo.tripod.web.annotation.AuthIgnore;
import com.niiwoo.tripod.web.vo.Empty;
import com.niiwoo.tripod.web.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;

/**
 * @Description: 平台相关
 * @Author:LiJian
 * @Date:2017/11/29
 */
@Slf4j
@AuthIgnore
@Api(tags = "平台相关(李健)", description = "/platform")
@RestController
@RequestMapping("/platform")
public class PlatformController {

    @Reference(version = "1.0.0")
    private StatisticsInfoDubboService statisticsInfoDubboService;
    @Autowired
    private PrivacyMasks privacyMasks;

    @Reference(version = "1.0.0")
    private TradeConfigDubboService tradeConfigDubboService;

    @Reference(version = "1.0.0")
    private ProjectLendDubboService projectLendDubboService;

    @PostMapping("/statistics")
    @ApiOperation("平台统计信息")
    public Result<PlatformStatisticsResponseVo> statistics() {
        log.info("statistics start");
        PlatformStatisticsResponseVo responseVO = new PlatformStatisticsResponseVo();
        StatisticsInfoResponseDTO statisticsInfoResponseDTO = statisticsInfoDubboService.queryStatisticsInfo();
        responseVO.setRegisterUserCount(Long.valueOf(statisticsInfoResponseDTO.getRegisterUserCount()));
        responseVO.setApplyLoanUserCount(Long.valueOf(statisticsInfoResponseDTO.getApplyLoanUserCount()));
        responseVO.setTodayRegisterUserCount(Long.valueOf(statisticsInfoResponseDTO.getTodayRegisterUserCount()));
        responseVO.setCumulativeTradeAmount(new BigDecimal(statisticsInfoResponseDTO.getCumulativeTradeAmount()));
        responseVO.setOperateDays(Long.valueOf(statisticsInfoResponseDTO.getOperateDays()));
        responseVO.setApplyLoanAmount(new BigDecimal(statisticsInfoResponseDTO.getApplyLoanAmount()));
        responseVO.setCumulativeUserProfit(new BigDecimal(statisticsInfoResponseDTO.getCumulativeUserProfit()));
        log.info("statistics.responseVO={}", JSON.toJSONString(responseVO));
        return Result.with(responseVO);
    }


    @PostMapping("/encryptionToDecrypt")
    @ApiOperation("数据加密解密")
    public Result<EncryptionToDecryptResponseVO> encryptionToDecrypt(@RequestBody @Valid EncryptionToDecryptRequestVO requestVO) {
        EncryptionToDecryptResponseVO responseVO = new EncryptionToDecryptResponseVO();
        String date = "";
        if (requestVO.getType().equals("2")) {
            date = privacyMasks.decryptPrivacy(requestVO.getData());
        } else if (requestVO.getType().equals("1")) {
            date = privacyMasks.encryptPrivacy(requestVO.getData());
        }
        responseVO.setData(date);
        return Result.with(responseVO);
    }

    @PostMapping("/getProjectIntroduce")
    @ApiOperation("获取标的介绍配置")
    public Result<ProjectIntroduceVO> getProjectIntroduce() {
        ProjectIntroduceDTO dto = tradeConfigDubboService.getProjectIntroduce();

        ProjectIntroduceVO vo = new ProjectIntroduceVO();
        vo.setPersonalProjectIntroduceImgUrl(dto.getPersonalProjectIntroduceImgUrl());
        vo.setCompanyProjectIntroduceImgUrl(dto.getCompanyProjectIntroduceImgUrl());

        return Result.with(vo);
    }

    @AuthIgnore
    @PostMapping("/signContract/Y29udHJhY3RSZXNpZ24=")
    @ApiOperation("居间借款合同补偿")
    public Empty signContract(@RequestBody @Valid SignContractRequestVO requestVO) {
        projectLendDubboService.signContract(requestVO.getProjectId());
        return Empty.getInstance();
    }

}
